<template>
  <a-modal :title="title" :width="850" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dealerName" required label="经销商名称">
          <a-input placeholder="请输入" v-model="model.dealerName" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name" required label="负责人">
          <a-input placeholder="请输入" v-model="model.name" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tel" required label="手机号">
          <a-input placeholder="请输入" v-model="model.tel" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark" required label="备注">
          <a-input placeholder="请输入" v-model="model.remark" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status" required label="状态">
          <a-radio-group v-model="model.status" name="radioGroup">
            <a-radio :value="0">启用</a-radio>
            <a-radio :value="1">禁用</a-radio>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { saveOrUpdateUser } from '@/api/marketingCenter/dealer'

export default {
  name: 'banner',
  data() {
    return {
      lxList: [
        {
          value: 1,
          name: '首焦图'
        },
        {
          value: 2,
          name: '横幅'
        }
      ],
      title: '',
      visible: false,
      model: {
        status: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        dealerName: [{ required: true, message: '请输入!' }],
        name: [{ required: true, message: '请输入!' }],
        // tel: [{ required: true, message: '请输入!' }],
        tel: [{ required: true, message: '请输入手机号码!' }, { validator: this.validateMobile }],
        remark: [{ required: true, message: '请输入!' }],
        status: [{ required: true, message: '请选择!' }]
      }
    }
  },
  created() {},
  methods: {
    // 校验手机号
    validateMobile(rule, value, callback) {
      if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)) {
        callback()
      } else {
        callback('请输入正确的手机号!')
      }
    },
    add() {
      this.edit({
        status: 0
      })
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          saveOrUpdateUser(this.model)
            .then(res => {
              if (res.success) {
                that.$message.success(res.msg)
                that.$emit('ok')
              } else {
                that.$message.warning(res.msg)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.resetFields()
    }
  }
}
</script>
